package com.wk.shiro;

import com.wk.entity.ManagerInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;
import org.apache.shiro.authc.AuthenticationException;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;


/*
* 验证码过滤器
* */
@Slf4j
public class KaptchaFilter extends FormAuthenticationFilter {

    public static final String DEFAULT_CAPTCHA_PARAM = "captcha";

    private String captchaParam = DEFAULT_CAPTCHA_PARAM;

    //登录验证
    @Override
    protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception
    {
        CaptchaUsernamePasswordToken token = createToken(request,response);
        try{
            log.info("KaptchaFilter.executeLogin");
            /* 图形验证码验证*/
            doCaptchaValidate((HttpServletRequest)request,token);
            Subject subject = getSubject(request,response);
            subject.login(token);//正常验证

            //验证成功，把用户信息放到session中
            ManagerInfo user = ShiroKit.getUser();
            ((HttpServletRequest)request).getSession().setAttribute("user",user);

            return onLoginSuccess(token,subject,request,response);
        }catch (AuthenticationException e){
            return onLoginFailure(token,e,request,response);
        }
    }

    @Override
    protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception{
        issueSuccessRedirect(request, response);

        return  false;
    }

    protected void issueSuccessRedirect(ServletRequest request, ServletResponse response) throws Exception
    {
        WebUtils.issueRedirect(request,response,"/",null,true);
    }

    //验证码校验
    protected void doCaptchaValidate(HttpServletRequest request,CaptchaUsernamePasswordToken token)
    {
        log.info("KaptchaFilter.doCaptchaValidate");
        String captcha = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
        log.info("session中的图形码字符串:" + captcha);

        //比对
        if(captcha == null || !captcha.equalsIgnoreCase(token.getCaptcha()))  //比较时忽略大小写
        {
            throw new IncorrectCredentialsException();
        }
    }

    @Override
    protected CaptchaUsernamePasswordToken createToken(ServletRequest request,ServletResponse response)
    {
        String username = getUsername(request);
        String password = getPassword(request);
        String captcha = getCaptcha(request);
        boolean rememberMe = isRememberMe(request);
        String host = getHost(request);
        return new CaptchaUsernamePasswordToken(username,password.toCharArray(),rememberMe,host,captcha);
    }

    public String getCaptchaParam(){ return captchaParam;}

    public void setDefaultCaptchaParam(String captchaParam)
    {
        this.captchaParam = captchaParam;
    }

    protected String getCaptcha(ServletRequest request)
    {
        return WebUtils.getCleanParam(request, getCaptchaParam());
    }

    //保存异常对象到request
    @Override
    protected void setFailureAttribute(ServletRequest request, AuthenticationException ae)
    {
        request.setAttribute(getFailureKeyAttribute(),ae);
    }
}
